package com.tre.jdevtemplateboot.web.interceptor;

import com.tre.jdevtemplateboot.common.constant.SysConstantToken;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JDev
 */
public class HeadToken {

    private HeadToken() {
    }

    public static String getHeadToken(HttpServletRequest request, String authHeaderKey, String authTypePrefix){
        //从header和请求参数中得到token
        String authHeader = request.getHeader(authHeaderKey);
        if (!StringUtils.hasLength(authHeader)){
            authHeader = request.getParameter(authHeaderKey);
        }
        //验证token
        if (!StringUtils.hasLength(authHeader) || !authHeader.startsWith(authTypePrefix)) {
            return null;
        }
        return authHeader.startsWith(SysConstantToken.JWT_AUTHOR_TYPE) ?
                authHeader.substring(SysConstantToken.JWT_AUTHOR_TYPE.length()) :
                authHeader.substring(authTypePrefix.length());
    }

}
